#include <string>
#include <shared_mutex>
#include <thread>
#include <iostream>

using namespace std;


std::shared_mutex g_mutex;
std::string g_str;

void readLoop() {
    while (true) {
        this_thread::sleep_for(chrono::milliseconds(1000));
        g_mutex.lock_shared();
        this_thread::sleep_for(chrono::milliseconds(1000));
        cout << g_str;
        g_mutex.unlock_shared();
    }
}

void writeLoop() {
    int number = 0;
    while (true) {
        this_thread::sleep_for(chrono::milliseconds(1000));
        g_mutex.lock();
        this_thread::sleep_for(chrono::milliseconds(10000));
        g_str = to_string(number++) + "\n";
        g_mutex.unlock();
    }
}

int main() {
    thread(writeLoop).detach();
    thread(readLoop).detach();
    thread(readLoop).detach();
    system("pause");
}
